#include "classifier.h"
#include "preprocessing.h"


Classifier::Classifier(void):_filePath(""),_zoomFactor(10),_pdfMgr(NULL),_showKey(""),_rootPath(""),_showType("")
{
	_pdfMgr = new PdfManager;
	_probability = new Probability;
	_processMgr = new PreProcessing;
}

Classifier::~Classifier(void)
{

}

void Classifier::classify()
{
	vector<string> testText = PdfManager::loadPdf(_filePath);
	_processMgr->dropStopWord(testText);
	_keyWord = _processMgr->keyWord(testText);

	string keyRet = "";
	for(int i = 0; i < _keyWord.size(); i++)
	{
		keyRet = keyRet + _keyWord[i] + "\n";
	}
	_showKey = keyRet;
	_showType = sortProbability().at(0).first;
}

string Classifier::getKeyList()
{
	return _showKey;
}

void Classifier::openPdfFile(string &filePath)
{
	_filePath = filePath;
}

void Classifier::setFilePath(string &filePath)
{
	_filePath = filePath;
}

string Classifier::getFilePath()
{
	return _filePath;
}

void Classifier::setRootPath(string &rootPath)
{
	_rootPath = rootPath;
}

string Classifier::getRootPath()
{
	return _rootPath;
}

string Classifier::getResult()
{
	return _showType;
}

float Classifier::calcProd(vector<string> X, string Cj)
{
	float ret = 1;

	for (int i = 0; i < X.size(); i++)
	{
		string Xi = X[i];

		ret = ret * _probability->calculatePxc(Xi, Cj)* _zoomFactor;
	}

	ret *= _probability->calculatePc(Cj);
	return ret;
}

struct compByVal{
	bool operator()(const pair<string, double>& lhs, const pair<string, double>& rhs)
	{
		return lhs.second > rhs.second;
	}
};

vector<pair<string, double>> Classifier::sortProbability()
{
	map	<string ,float> retMap;
	vector<string> folderName = _traningMgr->getFolderName();
	vector<float> f(folderName.size());

	for (int i = 0; i < folderName.size(); i++) {
		f[i] = calcProd(_keyWord, folderName[i]);
		retMap[folderName[i]] = f[i];
	}

	vector<pair<string,double>> sortWordVec(retMap.begin(), retMap.end());
	sort(sortWordVec.begin(), sortWordVec.end(),compByVal());

	return sortWordVec;
}